<template>
	<view class="selectMaterial">
		<!-- 选中的材料 -->
		<navBar title="已选材料" goBackUrl="/subpackage-materials/pages/materials/materialsCome/components/comeMaterialUnit" @goBack="goBack"></navBar>
		
		<text class="del">删除</text>
		
		<scroll-view scroll-y="true" class="scroll-box">
			<view class="content">
				<view class="info">
					<checkbox-group @change="checkboxChange">
						<label v-for="(item,index) in tableData" :key="index" class="label">
							<view class="item" >
								<view class="header">{{item.name}}/{{item.deviceTypeName}}</view>
								<view class="item-body">
									<view class="item-nav">
										<view class="name">规格型号：</view>
										<view class="value">{{item.specificationModel}}</view>
									</view>
									<view class="item-nav">
										<view class="name">过保日期：</view>
										<view class="value">{{item.time}}</view>
									</view>
									<view class="item-nav">
										<view class="name">基地名称：</view>
										<view class="value">{{item.stationName}}</view>
									</view>
							<!-- 		<view class="item-nav">
										<view class="name">存储区域：</view>
										<view class="value">{{item.storageAreaName}}</view>
									</view> -->
									<view class="item-nav">
										<view class="name">库存量：</view>
										<view class="value">{{item.num}}吨</view>
									</view>
									<view class="item-nav m40">
										<view class="name">出库量：</view>
										<view class="value right">
											<u-number-box class="number"  :min="0" :max="item.num" v-model="item.outNum" @change="curr=>comeNumChange(curr,item,index)" color="#323233"></u-number-box>
											<text class="unit">吨</text>
										</view>
									</view>
									
									<checkbox  :value="item.id" class="color round checkbox" />
								</view>
							</view>
						</label>
					</checkbox-group>
				</view>
			</view>
		</scroll-view>
		
		<view class="button" @click="submit">保存</view>
	</view>
</template>

<script>
	import { mapMutations,mapState } from "vuex"
	export default {
		name: "selectMaterial",
		data() {
			return {
				tableData:[],
				materialIdList:[],
			}
		},
		onLoad() {
			// console.log('onload')
			// this.$$off('data-check')
			// this.$$on('data-check',data=>{
			// 	console.log(data)
			// })
			// #ifdef APP-NVUE
			const eventChannel = this.$scope.eventChannel; // 兼容APP-NVUE
			// #endif
			// #ifndef APP-NVUE
			const eventChannel = this.getOpenerEventChannel();
			// #endif
			eventChannel.on('acceptDataFromOpenerPage', data=> {
				this.init(data)
			})			
		},
		methods: {
			...mapMutations('materials', ["getMaterialsComeList"]),
			comeNumChange(curr,item,index){
				this.tableData[index].outNum=curr.value
				//获取目前添加的这个对象的id对应的下表
			},
			init(data){
				this.tableData=data
			},
			
			checkboxChange(e){
				this.materialIdList=e.detail.value
			},
			// 返回上一级
			goBack(url){
				uni.navigateBack({
					url: url
				})
			},
			submit(){
				if(this.materialIdList.length===0){
					uni.showToast({
						title:'请先选择材料',
						icon:'error'
					})
					return
				}
				let res=[]
				
				this.materialIdList.forEach(item=>{
					this.tableData.forEach(i=>{
						console.log(item)
						console.log(i.id)
						
						if(i.id==item && i.outNum>0){
							res.push(i)
						}
					})
				})
				if(!res ||res.length==0){
					uni.showToast({
						title:'请先选择材料',
						icon:'error'
					})
					return
				}
				this.getMaterialsComeList(res)
				// this.$$emit('data-submit', res)
				uni.navigateTo({
					url:"/subpackage-materials/pages/materials/materialsCome/materialsCome"
				})
				
			}
		}
		
	}
</script>

<style lang="scss" scoped>
	.selectMaterial{
		padding-bottom: 105rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		position: relative;
		/* #ifdef MP-WEIXIN */
			padding-top: 180rpx;
		/* #endif */
		.scroll-box{
			max-height: calc(100vh - 274rpx);
			padding-top: 10rpx;
		}
		.content{
			background-color: #F5F7FA;
			padding: 24rpx 32rpx;
		}
		.del{
			position: absolute;
			top: 47rpx;
			right: 50rpx;
			font-size: 32rpx;
			font-weight: 500;
			color: #FF6A6A;
		}
		.info{
			.item{
				width: 100%;
				background: #FFFFFF;
				margin-bottom: 20rpx;
				.header{
					font-size: 30rpx;
					line-height: 88rpx;
					font-weight: bold;
					color: #445469;
					height: 88rpx;
					border-bottom: 2rpx solid #E2E5ED;
					padding-left: 35rpx;
				}
				.item-body{
					position: relative;
					padding: 26rpx 35rpx;
					padding-bottom: 10rpx;
					
					.item-nav{
						display: flex;
						align-items: center;
						font-size: 28rpx;
						font-weight: 500;
						color: #445469;
						margin-bottom: 18rpx;
						.name{
							width: 160rpx;
						}
						.value{
							flex: 1;
						}
						.right{
							display: flex;
							align-items: center;
							justify-content: flex-end;
						}
						.unit{
							margin-left: 10rpx;
						}
					}
					.m40{
						justify-content: space-between;
						margin-top: 20rpx;
					}
				}
			}
			.checkbox{
				position: absolute;
				top: 25rpx;
				right: 25rpx;
			}
		}
		
		.button{
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
			height: 105rpx;
			line-height: 105rpx;
			color: #FFFFFF;
			font-size: 32rpx;
			font-weight: 500;
			background-color: #2F80ED;
			text-align: center;
		}
	}
</style>
